package com.bruce.codeFactory.practice;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

/**
 * @author luotuan
 * @Description
 * @create 2019-03-20 14:06
 **/
public class Encryptor {
    Key key;

    Encryptor(byte[] key) {
        if (key.length != 16) throw new IllegalArgumentException();
        this.key = new SecretKeySpec(key, "AES");
    }

    // the output is sent to users
    byte[] encrypt(byte[] src) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] iv = cipher.getIV(); // See question #1
        assert iv.length == 12; // See question #2
        byte[] cipherText = cipher.doFinal(src);
        assert cipherText.length == src.length + 16; // See question #3
        byte[] message = new byte[12 + src.length + 16]; // See question #4
        System.arraycopy(iv, 0, message, 0, 12);
        System.arraycopy(cipherText, 0, message, 12, cipherText.length);
        return message;
    }

    // the input comes from users
    byte[] decrypt(byte[] message) throws Exception {
        if (message.length < 12 + 16) throw new IllegalArgumentException();
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        GCMParameterSpec params = new GCMParameterSpec(128, message, 0, 12);
        cipher.init(Cipher.DECRYPT_MODE, key, params);
        return cipher.doFinal(message, 12, message.length - 12);
    }

    public static void main(String[] args) throws Exception {
        try {
            System.out.println("VogXAWJuAN1Qa7ubhR86cg==".length());
            KeyGenerator kg= KeyGenerator.getInstance("AES");
            //初始化密钥生成器，AES要求密钥长度为128位、192位、256位
            kg.init(128);
            SecretKey secretKey=kg.generateKey();
            System.out.println("密钥："+ Base64.encodeBase64String(secretKey.getEncoded()));

            SecretKey key=new SecretKeySpec("VogXAWJuAN1Qa7ub".getBytes(),"AES");

            String txt = "testtxt";
            System.out.println("原文："+txt);

            Cipher cipher= Cipher.getInstance("AES/GCM/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] iv = cipher.getIV();
            assert iv.length == 12;
            byte[]  encryptData = cipher.doFinal(txt.getBytes());
            assert encryptData.length == txt.getBytes().length + 16;
            byte[] message = new byte[12 + txt.getBytes().length + 16];
            System.arraycopy(iv, 0, message, 0, 12);
            System.arraycopy(encryptData, 0, message, 12, encryptData.length);
            System.out.println("加密后："+ Base64.encodeBase64String(message));


            if (message.length < 12 + 16) throw new IllegalArgumentException();
            GCMParameterSpec params = new GCMParameterSpec(128, message, 0, 12);
            cipher.init(Cipher.DECRYPT_MODE, key, params);
            byte[]  decryptData = cipher.doFinal(message, 12, message.length - 12);
            System.out.println("解密后："+new String(decryptData));


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
